<template>
  <div class="app-container">
    <el-card class="search-card">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="专业" prop="subjectId">
            <el-select v-model="queryParams.subjectId" placeholder="请选择专业" clearable>
              <el-option
                v-for="subject in subjectList"
                :key="subject.id"
                :label="subject.subjectName"
                :value="subject.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="班级名称" prop="clazzName">
            <el-input
              v-model="queryParams.clazzName"
              placeholder="请输入班级名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="年级" prop="grade">
            <el-select v-model="queryParams.grade" placeholder="请选择年级" clearable>
              <el-option
                v-for="g in gradeList"
                :key="g"
                :label="g"
                :value="g"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学制" prop="educationalSystem">
            <el-select v-model="queryParams.educationalSystem" placeholder="请选择学制" clearable>
              <el-option
                v-for="dict in dict.type.educational_system"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="学位类型" prop="clazzType">
            <el-select v-model="queryParams.clazzType" placeholder="请选择学位类型" clearable>
              <el-option
                v-for="dict in dict.type.clazz_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    </el-card>

    <el-card class="table-card">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['clazz:info:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['clazz:info:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['clazz:info:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="学院名称" align="center" prop="depName" />
          <el-table-column label="专业" align="center" prop="subjectName" />
          <el-table-column label="班级名称" align="center" prop="clazzName" />
          <el-table-column label="班级代码" align="center" prop="clazzCode" />
          <el-table-column label="年级" align="center" prop="grade" />
          <el-table-column label="学制" align="center" prop="educationalSystem">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.educational_system" :value="scope.row.educationalSystem"/>
            </template>
          </el-table-column>
          <el-table-column label="班级描述" align="center" prop="clazzDescribe" />
          <el-table-column label="学位类型" align="center" prop="clazzType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.clazz_type" :value="scope.row.clazzType"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['clazz:info:edit']"
              >修改</el-button>
              <br/>
              <router-link :to="'/clazz/detail/index/' + scope.row.id">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                >学生管理</el-button>
              </router-link>
              <br/>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['clazz:info:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
    </el-card>

    <!-- 添加或修改班级信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="专业" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择专业" clearable>
            <el-option
              v-for="subject in subjectList"
              :key="subject.id"
              :label="subject.subjectName"
              :value="subject.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称" prop="clazzName">
          <el-input v-model="form.clazzName" placeholder="请输入班级名称" />
        </el-form-item>
        <el-form-item label="班级代码" prop="clazzName">
          <el-input v-model="form.clazzCode" placeholder="请输入班级代码" />
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-select v-model="form.grade" placeholder="请选择年级" clearable>
            <el-option
              v-for="g in gradeList"
              :key="g"
              :label="g"
              :value="g"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学制" prop="educationalSystem">
          <el-select v-model="form.educationalSystem" placeholder="请选择学制">
            <el-option
              v-for="dict in dict.type.educational_system"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学位类型" prop="clazzType">
          <el-select v-model="form.clazzType" placeholder="请选择学位类型">
            <el-option
              v-for="dict in dict.type.clazz_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级描述" prop="clazzDescribe">
          <el-input v-model="form.clazzDescribe" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, getGradeList } from "@/api/information/clazzInfo";
import { getSubjectByLoginDepId } from '@/api/information/subject'

export default {
  name: "Info",
  dicts: ['educational_system', 'clazz_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 班级信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depId: null,
        subjectId: null,
        clazzName: null,
        grade: null,
        educationalSystem: null,
        clazzType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        subjectId: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        clazzName: [
          { required: true, message: "班级名称不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ],
        educationalSystem: [
          { required: true, message: "学制不能为空", trigger: "change" }
        ],
        clazzType: [
          { required: true, message: "学位类型不能为空", trigger: "change" }
        ],
      },
      subjectList: [],
      gradeList: []
    };
  },
  created() {
    this.getList();
    this.findSubjectList();
    this.findGradeList();
  },
  methods: {
    findGradeList(){
      getGradeList().then(response => {
        this.gradeList = response.data;
      })
    },
    findSubjectList(){
      getSubjectByLoginDepId().then(response => {
        this.subjectList = response.data;
      })
    },
    /** 查询班级信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        depId: null,
        subjectId: null,
        clazzName: null,
        grade: null,
        educationalSystem: null,
        clazzDescribe: null,
        clazzType: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改班级信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除班级信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('clazz/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
